package main

import (
	"bufio"
	"log"
	"math/rand"
	"os"
	"strconv"
)

const (
	lines = 50000
)

var keyIndex = -1

func main() {
	err := os.RemoveAll("./data/")
	if err != nil {
		log.Fatal(err)
	}
	err = os.MkdirAll("./data/", 0644)
	if err != nil {
		log.Fatal(err)
	}
	file, err := os.OpenFile("./data/data.txt", os.O_CREATE|os.O_RDWR, 0644)
	defer file.Close()
	if err != nil {
		log.Fatal(err)
	}
	writer := bufio.NewWriter(file)
	if err != nil {
		log.Fatal(err)
	}
	for i := 0; i < lines; i++ {
		s := randomString()
		_, err = writer.WriteString(s + "\n")
		if err != nil {
			log.Fatal(err)
		}
	}
	err = writer.Flush()
	if err != nil {
		log.Fatalln(err)
	}
	//当前协程生成完临时文件过后写入信号
}

func randomInt(min, max int) int {
	return min + rand.Intn(max-min)
}

func randomString() string {
	b := make([]byte, 0)
	chars := rand.Intn(90)
	//字符数至少大于十
	if chars <= 10 {
		chars += 10
	}
	for i := 0; i < chars; i++ {
		if rand.Intn(10)%2 == 0 {
			b = append(b, byte(randomInt(65, 90)))
			continue
		}
		b = append(b, byte(randomInt(97, 122)))
	}
	keyIndex++
	return strconv.Itoa(keyIndex) + "," + string(b)
}
